/**
 * 
 */
package com.jm.web.builder;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.Utils;

import java.io.IOException;
import java.sql.ResultSet;
/**
 * @author yanjie
 *
 * 2011/9/22 上午9:38:57
 * 
 */
public class MisBuilder extends BaseBuilder{
	protected String sysCode = "";
	@Override
	public void RenderEnd() {
		String sysCode = this.getBaseTag().GetExtValue("sysCode","");
		Utils utils = this.getBaseTag().getPage().getUtils(); 
		try {
			String reqURL = utils.getRequest().getRequestURI().trim() ;//取到頁面的url
			if(reqURL.length() > 0){
				reqURL = reqURL.split("/")[reqURL.split("/").length-1];	
			}
			StringBuilderEx sql = new StringBuilderEx();
			StringBuilderEx sb = new StringBuilderEx();
			
			sql.Append("select * from sys..sys002 where sysCode='%1$s'",reqURL);
			
			ResultSet rs = utils.executeQuery("info",sql.toString());
			
			if(rs.next()){
				sb.Append("<a href=%1$s style=\"color: #0000FF;font-size: 12px\" target=\"_blank\">%2$s</a>",utils.getRsString(rs, "url"),"\u7cfb\u7d71\u6216\u898f\u5247\u8aaa\u660e");
			}
			sql.Reset();
			//驗證登陸人是否有超級權限
			sql.Append("select hrnum from sys..sys002a where hrnum ='%1$s'",utils.getHrNum());
			rs = utils.executeQuery("info",sql.toString());
			if(rs.next()){
				sb.Append("<table class=\"mis\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"2\" style=\"text-align: left; font-size: 12px;\">");
				sb.Append("<thead>");
				sb.Append("<tr><td>");
				sb.Append("<div id=\"TextMis\" style=\"height:auto;margin-top:1px;width:100%\">");
				sb.Append("<img onclick=\"$(this).toggleClass('ax-tree-elbow-minus');$('#mis_body').toggle();$P.refresh();\" class=\"ax-tree-elbow-plus \" src=\""+this.getBaseTag().getPage().getContextPath()+"/include/images/default/s.gif\" style=\"cursor: pointer;\" width=\"16px\" height=\"16px\" />");
				sb.Append("<span style='background-color: rgb(255, 0, 0); font-weight: bold; color: rgb(255, 255, 255); font-size: 14px;'> MIS Notices </span>");
				sb.Append("</td></tr>");
				sb.Append("</thead>");
				sb.Append("<tbody style=\"display:none;\" id=\"mis_body\">");
				if(this.getBaseTag().getBodyContent()!=null&&!"".equals(this.getBaseTag().getBodyContent().getString())){
					sb.Append("<tr><td>%1$s</td></tr>",this.getBaseTag().getBodyContent().getString());
				}
				
				sysCode.replaceAll(" ", "");//去除空格
				if(!"".equals(sysCode)){
					String[] ary = sysCode.split(",");
					//循環所有權限
					for (int i = 0;i < ary.length; i++) {
						sql.Reset();
						sql.Append("select rem from sys..sys002 where sysCode = '%1$s'",ary[i]);
						rs = utils.executeQuery("info",sql.toString());
						String rem = "";
						if(rs.next()) {
							rem = utils.getRsString(rs, "rem");
						}
						sb.Append("<tr><td>");
						sb.Append("<div>");
						sb.Append("<a target='blank' href='");
						sb.Append(utils.getRequest().getContextPath());
						sb.Append("/acl/acl003S.jsp?sysCode=");
						sb.Append(ary[i]);
						sb.Append("'>");
						sb.Append(ary[i]);
						sb.Append("</a> : ");
						sb.Append(rem);
						sb.Append("</div>");
						sb.Append("</td></tr>");
					}
				}
				sb.Append("</tbody>");
				sb.Append("</table>");
			}	
			this.getOut().write(sb.toString());
		} catch (Exception e) {
			try {
				this.getOut().write("<span style='background-color: rgb(255, 0, 0); font-weight: bold; color: rgb(255, 255, 255); font-size: 18px;'>mis tag Error !!</span>");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			utils.cleanUp();
		}
	}
	
	public String getSysCode() {
		return sysCode;
	}

	public void setSysCode(String sysCode) {
		this.sysCode = sysCode;
	}
}
